package com.sys.SysFeedingRecords.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.sys.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sys.SysFeedingRecords.mapper.SysInventoryMapper;
import com.sys.SysFeedingRecords.domain.SysInventory;
import com.sys.SysFeedingRecords.service.ISysInventoryService;

/**
 * 物资库存Service业务层处理
 * 
 * @author sys
 * @date 2025-09-19
 */
@Service
public class SysInventoryServiceImpl implements ISysInventoryService 
{
    @Autowired
    private SysInventoryMapper sysInventoryMapper;

    /**
     * 查询物资库存
     * 
     * @param id 物资库存主键
     * @return 物资库存
     */
    @Override
    public SysInventory selectSysInventoryById(Long id)
    {
        return sysInventoryMapper.selectSysInventoryById(id);
    }

    /**
     * 查询物资库存列表
     * 
     * @param sysInventory 物资库存
     * @return 物资库存
     */
    @Override
    public List<SysInventory> selectSysInventoryList(SysInventory sysInventory)
    {
        return sysInventoryMapper.selectSysInventoryList(sysInventory);
    }


    /*
    * 三级联动选择
    * */
    // com.sys.SysFeedingRecords.service.impl.SysInventoryServiceImpl

    @Override
    public List<String> selectCategoryList() {
        return sysInventoryMapper.selectCategoryList();
    }

    @Override
    public List<String> selectMaterialNamesByCategory(String category) {
        return sysInventoryMapper.selectMaterialNamesByCategory(category);
    }

    @Override
    public List<String> selectBatchNumbersByMaterialAndCategory(String category, String materialName) {
        // 关闭分页功能
        PageHelper.clearPage();
        return sysInventoryMapper.selectBatchNumbersByMaterialAndCategory(category, materialName);
    }

    @Override
    public List<Map<String, Object>> selectBatchNumbersWithIdsByMaterialAndCategory(String category, String materialName, String batchNumber) {
        // 关闭分页功能
        PageHelper.clearPage();
        return sysInventoryMapper.selectBatchNumbersWithIdsByMaterialAndCategoryAndMaterialCode(category, materialName, batchNumber);
    }

    /**
     * 新增物资库存
     * 
     * @param sysInventory 物资库存
     * @return 结果
     */
    @Override
    public int insertSysInventory(SysInventory sysInventory)
    {
//        //入库-出库
//        sysInventory.setCurrentStock(sysInventory.getTotalIn().subtract(sysInventory.getTotalOut()));
//        sysInventory.setCreateTime(DateUtils.getNowDate());
//        return sysInventoryMapper.insertSysInventory(sysInventory);
        // 对 totalIn 和 totalOut 进行空值处理
        BigDecimal totalIn = sysInventory.getTotalIn() == null ? BigDecimal.ZERO : sysInventory.getTotalIn();
        BigDecimal totalOut = sysInventory.getTotalOut() == null ? BigDecimal.ZERO : sysInventory.getTotalOut();

        // 安全执行减法运算
        sysInventory.setCurrentStock(totalIn.subtract(totalOut));
        sysInventory.setCreateTime(DateUtils.getNowDate());
        return sysInventoryMapper.insertSysInventory(sysInventory);
    }

    /**
     * 修改物资库存
     * 
     * @param sysInventory 物资库存
     * @return 结果
     */
    @Override
    public int updateSysInventory(SysInventory sysInventory)
    {
        sysInventory.setUpdateTime(DateUtils.getNowDate());
        return sysInventoryMapper.updateSysInventory(sysInventory);
    }

    /**
     * 批量删除物资库存
     * 
     * @param ids 需要删除的物资库存主键
     * @return 结果
     */
    @Override
    public int deleteSysInventoryByIds(Long[] ids)
    {
        return sysInventoryMapper.deleteSysInventoryByIds(ids);
    }

    /**
     * 删除物资库存信息
     * 
     * @param id 物资库存主键
     * @return 结果
     */
    @Override
    public int deleteSysInventoryById(Long id)
    {
        return sysInventoryMapper.deleteSysInventoryById(id);
    }
}
